#include	"a_global_vars.h"
paxos_msg *accept_msg;
//------------------------------------------------------------------------------
void coordinator_execute_phase_2_a(int i, int r) {

	int VALUE;

	paxos_instances[(i % INTERNAL_BUFFER_SIZE)].rnd = 0;
	paxos_instances[(i % INTERNAL_BUFFER_SIZE)].vrnd = 0;
	paxos_instances[(i % INTERNAL_BUFFER_SIZE)].vval = 0;
	paxos_instances[(i % INTERNAL_BUFFER_SIZE)].crnd = 1;// before =0
	paxos_instances[(i % INTERNAL_BUFFER_SIZE)].cval = 0;

	if (paxos_instances[(i % INTERNAL_BUFFER_SIZE)].crnd == r && paxos_instances[(i % INTERNAL_BUFFER_SIZE)].cval == 0) {
		VALUE = data[(i % INTERNAL_BUFFER_SIZE)].packet.pack_id;
		paxos_instances[(i % INTERNAL_BUFFER_SIZE)].cval = VALUE;

		accept_msg->instance_number = i;
		accept_msg->round_number = r;
		accept_msg->type = PHASE_2_A;
		accept_msg->max_vval = -1;
		accept_msg->max_vrnd = -1;
		accept_msg->VALUE = VALUE;
		accept_msg->acceptor_count = 0;
		execute_phase_2_b(accept_msg);
	}
}
//------------------------------------------------------------------------------

/*void coordinator_execute_phase_1_a(int num)
 {

 paxos_msg *prepare_msg;
 char buffer[sizeof(paxos_msg)];

 prepare_msg=(paxos_msg*)malloc(sizeof(paxos_msg));
 data[COORDINATOR_ID][num].round_number++;

 if(paxos_instances[num].crnd < data[COORDINATOR_ID][num].round_number)
 {
 paxos_instances[num].crnd=data[COORDINATOR_ID][num].round_number;

 prepare_msg->instance_number=data[COORDINATOR_ID][num].instance_id;
 prepare_msg->round_number=paxos_instances[num].crnd;
 prepare_msg->type=PHASE_1_A;
 prepare_msg->acceptor_count=0;

 prepare_msg->max_vrnd=-1;
 prepare_msg->max_vval=-1;

 memcpy(buffer, prepare_msg, sizeof(paxos_msg));
 if(send(successor_sid, buffer, sizeof(paxos_msg),0) <0 )
 {
 perror("PHASE_1_A: sendidng failed.");
 exit(-1);
 }
 }

 }*/

//------------------------------------------------------------------------------

/*void coordinator_phase_recognizer(int sd, short event, void * arg)
 {
 char buffer[sizeof(paxos_msg)];
 int status;
 //printf("coordnator: before receive: \n");
 status=recv(sd, buffer, sizeof(paxos_msg), MSG_WAITALL);

 if(status<=0)
 {
 perror("coordinator: problem receiving\n");
 exit(-1);
 }
 //	if(((paxos_msg*)buffer)->type==PHASE_1_A)
 //		coordinator_execute_phase_2_a((paxos_msg*)buffer);
 //-----------------------
 if(((paxos_msg*)buffer)->type==PHASE_2_A)
 learner_deliver((paxos_msg*)buffer);

 //include this instance number in the multicast//may be just add the instance id to an array. and in multicasting mlticast the arraybut how long this array must be???

 //}
 */
